<?php
/**
 * Created by PhpStorm.
 * User: deng
 * Date: 2016/4/18
 * Time: 16:45
 * 粉粉闺蜜机器人
 */
class RobotController {

    protected $userInfo = null;
    protected $result = [];

    protected $platform = [
        0 => 'common',
        1 => 'ios',
        2 => 'android'
    ];

    protected $action = [
        1 => 'pinksns://community/follow/list',//社区关注页面
        2 => 'pinksns://community/topic/hot',//热门话题
        3 => 'pinksns://chatroom/chatroom',//聊天室
        4 => 'pinksns://channel/biz/duiba/redirect?redirect=%s&url=%s',//需要format, 默认福利社
        5 => 'http://d.fenfenriji.com/web/act/banner/banners.html?signature=1',//精选回顾
        6 => 'pinksns://diary/write',//写日记
        7 => 'pinksns://diary/self',//我的记录
        8 => 'pinksns://day_sentence/detail', //每日一语
        9 => 'pinksns://weather/detail',//天气
        10 => 'pinksns://planner/create',//手账
        11 => 'pinksns://user/login',//登录注册
        12 => 'pinksns://diary/sync',//同步
        13 => 'pinksns://mall/skin',//主题馆
        14 => 'pinksns://mall/paper',//信纸铺
        15 => 'pinksns://mall/emotion',//表情坊
        16 => 'pinksns://community/hot/list',//精选列表
        17 => 'pinksns://community/recent/list',//新鲜列表
        18 => 'pinksns://mall/pink',//粉币乐园
        19 => '',//自定义
    ];

    protected $typeGroup = [
        0 => 'activity',
        1 => 'everyDayLogin',
        2 => 'less7DayNotBoot',
        3 => 'over7DayNotBoot',
        4 => 'guest'
    ];

    function __construct(){
        //返回结果模板格式
        $this->result = [
            'mobileIsBind' => false,
            //消息组
            'messageGroup' => []
        ];
    }

    function callApi($param){
        $action = isset($param['action']) ? trim($param['action']) : 'index';
        return $this->$action($param);
    }

    /**
     * 获取所有的机器人消息
     * @return array|bool
     */
    function index(){
        $platformNO = $this->getPlatformNO();
        $robotModel = new RobotMsgModel();
        $result = $robotModel->getAll();
        if(empty($result)){
            AC::$errorNo    =   'RM001';//机器人消息为空
            return  FALSE;
        }
        foreach ($result as $item){
            //检查是否为通用信息
            if ($item['platform']!=0 && $platformNO != $item['platform']){
                continue;
            }

            $item['type'] = $this->group2Type($item['group']);
            $item['groupInfo'] = $this->typeGroup[$item['group']];
            if ($item['action'] == 0){
                $item['link'] = false;
            }else{
                $item['link'] = true;
                $item['action'] = $this->actionSprintf($item['action'], json_decode($item['json_param'], true));
            }
            //处理活动的数据
            /*if ($item['group'] == 0){
                $json = json_decode($item['json_param'], true);
                if (isset($json['action'])){
                    $item['action']=$json['action'] ;
                    $item['link'] = true;
                }
            }*/

            unset($item['status']);
            unset($item['id']);
            $group[$item['group']][] = $item;//将相同的group归并到一组中
        }
        foreach ($group as $index=>$v){
            $bigGroup[] = [
                'group' => $index,
                'groupArray' => $v
            ];
        }
        $this->result['messageGroup'] = $bigGroup;
        $this->result['mobileIsBind'] = $this->isBindMobile();
        return $this->result;
    }

    /**
     * 消息组对应的消息类型
     * @param num $group
     * @return string
     */
    function group2Type($group){
//        $activity = [0];
//        $duration = [1];
//        $normal = [2,3,4];
        switch ($group){
            case 0:return 'activity';
            case 1:return 'duration';
            case 2:
            case 3:
            case 4:return 'normal';
        }
    }

    /**
     * 是否绑定手机
     * @return bool
     */
    function isBindMobile(){
        $user = new UserController();
        $connent = $user->getConnent();
        $bind = false;
        //如果用户未登录，处理
        /*if (in_array(AC::$errorNo, ['UC001'])){
            AC::$errorNo = null;
            $connent = [];
        }*/
        foreach ($connent['connents'] as $v){
            $v['type'] == 'mobile' && $bind = true;
        }
        return $bind;
    }

    /**
     * 格式化action
     * @param num $actionNO
     * @param array $arg
     * @return mixed|string
     */
    function actionSprintf($actionNO, $arg=[]){
        switch ($actionNO){
            case 4://兑吧
                return sprintf(
                    $this->action[4],
                    isset($arg['redirect']) ? $arg['redirect'] : '1',
                    isset($arg['url']) ? $arg['url'] : 'http%3A%2F%2Fwww.duiba.com.cn%2Fchome%2Findex'
                );
            case 19://自定义
                return isset($arg['action']) ? trim($arg['action']) : '';
            default:
                return $this->action[$actionNO];
        }
    }

    /**
     * 获取平台代号
     * @return int
     */
    function getPlatformNO(){
        $platform = strtolower(AC::$appInfoList[2]);
        $r = array_flip($this->platform);
        return isset($r[$platform]) ? $r[$platform] : 0;
    }

    function __call($name, $arguments){
        AC::$errorNo    =   'SA004';
        return  ' Error : call action is not exists in RobotController! ';
    }

}